﻿using Puerts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace voidbox.Code.JSEngine
{
    public enum CmdInfo
    {
        //0x00 Test & Debug
        debug_Test = 0x00,
        debug_Log = 0x01,
        debug_Set_ConsoleLogFunc,
        debug_Set_ConsoleWarnFunc,
        debug_Set_ConsoleErrorFunc,
        //0x10 system
        sys_GetVersionString = 0x10,
        sys_RunApp,
        sys_ExitApp,
        sys_LoadBinaryInApp,
        sys_LoadTextInApp,
        //0x20 app
        app_GetRootPath = 0x20,
        app_LoadBinary,
        app_LoadText,
        app_GetPixelWidth,
        app_GetPixelHeight,
        app_SetGameUpdate,
        app_SetGameRender,
        app_SetClearColor,
        app_SetOnKey,
        app_SetOnPoint,
        //0x30 image
        imageData_SetCtor = 0x30,
        imageData_Create,
        imageData_Decode,
        //0x40 texture
        texture_SetCtor = 0x40,
        texture_Create,
        texture_CreateFromImageData,
        texture_GetWhite,
        texutre_Upload,
        texture_Draw,
        //0x50 sprite
        sprite_SetCtor = 0x50,
        sprite_Create,
        sprite_GetLogo,
        sprite_GetWhite,
        sprite_Draw,
        //0x60 font
        font_SetCtor = 0x60,
        font_Create,
        font_GetSprite,
        font_GetCharWidth,
        
        //0x0100 Sound
        soundsys_SetSoundVol = 0x0100,//系统音量，sound&bgm
        soundsys_SetBGMVol,
       
        soundsys_PlayBGM,
        soundsys_StopBGM,

        sound_SetCtor,
        sound_Create,
        sound_Play,
        bgm_SetCtor,
        bgm_Create,


        //0x0200 Input
        //Touch&Mouse
    }
    public class JsFuncUpdate
    {
        public readonly IntPtr envPtr;
        public readonly IntPtr funcPtr;
        public JsFuncUpdate(IntPtr envPtr, IntPtr funcPtr)
        {
            this.envPtr = envPtr;
            this.funcPtr = funcPtr;
        }
        public void InvokeUpdate(double delta)
        {
            PuertsDLL.PushNumberForJSFunction(this.funcPtr, delta);
            PuertsDLL.InvokeJSFunction(this.funcPtr, false);
        }
    }
    public class JsFuncRender
    {
        public readonly IntPtr envPtr;
        public readonly IntPtr funcPtr;
        public JsFuncRender(IntPtr envPtr, IntPtr funcPtr)
        {
            this.envPtr = envPtr;
            this.funcPtr = funcPtr;
        }
        public void InvokeRender()
        {
            //PuertsDLL.PushNumberForJSFunction(this.funcPtr, delta);
            PuertsDLL.InvokeJSFunction(this.funcPtr, false);
        }
    }
    public class JsFuncOnKey
    {
        public readonly IntPtr envPtr;
        public readonly IntPtr funcPtr;
        public JsFuncOnKey(IntPtr envPtr, IntPtr funcPtr)
        {
            this.envPtr = envPtr;
            this.funcPtr = funcPtr;
        }
        public void InvokeOnKey(int key, bool press)
        {
            PuertsDLL.PushNumberForJSFunction(this.funcPtr, key);
            PuertsDLL.PushBooleanForJSFunction(this.funcPtr, press);
            PuertsDLL.InvokeJSFunction(this.funcPtr, true);
        }
    }
    public class JsFuncOnPoint
    {
        public readonly IntPtr envPtr;
        public readonly IntPtr funcPtr;
        public JsFuncOnPoint(IntPtr envPtr, IntPtr funcPtr)
        {
            this.envPtr = envPtr;
            this.funcPtr = funcPtr;
        }
        public void InvokeOnPoint(int touchid, bool press,  float x, float y)
        {
            PuertsDLL.PushNumberForJSFunction(this.funcPtr, touchid);
            PuertsDLL.PushBooleanForJSFunction(this.funcPtr, press);
            PuertsDLL.PushNumberForJSFunction(this.funcPtr, x);
            PuertsDLL.PushNumberForJSFunction(this.funcPtr, y);
            PuertsDLL.InvokeJSFunction(this.funcPtr, true);
        }
    }
    public class JsFuncCreateImageData
    {
        public readonly IntPtr envPtr;
        public readonly IntPtr funcPtr;
        public JsFuncCreateImageData(IntPtr envPtr, IntPtr funcPtr)
        {
            this.envPtr = envPtr;
            this.funcPtr = funcPtr;
        }
        public IntPtr InvokeCreate(uint width, uint height, bool gray, byte[] data)
        {
            PuertsDLL.PushNumberForJSFunction(this.funcPtr, width);
            PuertsDLL.PushNumberForJSFunction(this.funcPtr, height);
            PuertsDLL.PushBooleanForJSFunction(this.funcPtr, gray);
            if (data == null)
                PuertsDLL.PushNullForJSFunction(this.funcPtr);
                //PuertsDLL.PushArrayBufferForJSFunction(this.funcPtr, null, 0);
            else
                PuertsDLL.PushArrayBufferForJSFunction(this.funcPtr, data, data.Length);
            return PuertsDLL.InvokeJSFunction(this.funcPtr, true);
        }
    }
}
